package com.qigao.mall.web.controller.admin.merchant;

import com.github.pagehelper.PageInfo;
import com.qigao.mall.api.SettleTotalService;
import com.qigao.mall.commons.dto.*;
import com.qigao.mall.commons.enums.Result;
import com.qigao.mall.commons.enums.ResultCode;
import com.qigao.mall.commons.exceptions.BusinessException;
import com.qigao.mall.commons.utils.excel.ExcelFormat;
import com.qigao.mall.commons.utils.excel.ExcelHeaderInfo;
import com.qigao.mall.commons.utils.excel.ExcelUtils;
import com.qigao.mall.web.controller.AbstractExcelController;
import com.qigao.mall.web.utils.RequestUtils;
import com.qigao.mall.web.utils.ValidatorUtil;
import com.qigao.mall.web.vo.admin.merchant.SettleAuditVo;
import com.qigao.mall.web.vo.admin.merchant.SettleCondVo;
import com.qigao.mall.web.vo.app.OrderQueryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.*;

@RestController
@RequestMapping("/admin/settleTotal/")
@Api(tags = "Admin商户操作结算单相关API" ,value = "Admin商户操作结算单相关API")
public class AdminSettleTotalController extends AbstractExcelController {

    private static Logger logger= LoggerFactory.getLogger(AdminSettleTotalController.class);
    @Resource
    private SettleTotalService settleTotalService;

    //1.结算审核
    @ApiOperation("结算审核")
    @PostMapping("/aduit")
    public Result auditSettle(@RequestBody SettleAuditVo settleAuditVo, HttpServletRequest request){
        ValidatorUtil.validateEntity(settleAuditVo);
        try{
            SettleAuditDto settleAuditDto=new SettleAuditDto();
            BeanUtils.copyProperties(settleAuditVo,settleAuditDto);
            settleAuditDto.setUserNo(RequestUtils.getCurrentUserNo(request));
            return settleTotalService.settleAudit(settleAuditDto);
        }catch (BusinessException be){
            logger.error("结算申请异常:{}",be);
            return Result.newError(be.getCode(),be.getMsg());
        }catch (Exception e){
            logger.error("结算申请发生异常:{}",e);
            return Result.newError(ResultCode.FAIL);
        }
    }
    @PostMapping("/listPage")
    @ApiOperation(value = "按照结算单状态查询列表", notes = "按照结算单状态查询列表")
    public Result<PageInfo<SettleTotalResultDto>> listPage(@RequestBody SettleCondVo settleCondVo, HttpServletRequest request){
        try{
            //参数校验
            SettleQueryDto settleQueryDto=new SettleQueryDto();
            BeanUtils.copyProperties(settleCondVo,settleQueryDto);
            if(StringUtils.isNotEmpty(settleCondVo.getDateBegin())){
                settleQueryDto.setDateBegin(settleCondVo.getDateBegin()+" 00:00:00");
            }
            if(StringUtils.isNotEmpty(settleCondVo.getDateEnd())){
                settleQueryDto.setDateEnd(settleCondVo.getDateEnd()+" 23:59:59");
            }
            return  settleTotalService.listPage(settleQueryDto);
        }catch (Exception e){
            logger.error("[按照结算单状态查询列表]发生异常:{}",e);
            return Result.newError(ResultCode.FAIL);
        }
    }

    @GetMapping("/export")
    @ApiOperation(value = "导出Excel", notes = "导出Excel")
    public void exportGet(SettleCondVo settleCondVo, HttpServletResponse response) {
        SettleQueryDto settleQueryDto=new SettleQueryDto();
        BeanUtils.copyProperties(settleCondVo,settleQueryDto);
        if(StringUtils.isNotEmpty(settleCondVo.getDateBegin())){
            settleQueryDto.setDateBegin(settleCondVo.getDateBegin()+" 00:00:00");
        }
        if(StringUtils.isNotEmpty(settleCondVo.getDateEnd())){
            settleQueryDto.setDateEnd(settleCondVo.getDateEnd()+" 23:59:59");
        }
        // 待导出数据
        List<SettleTotalResultDto> orderResultDtos = settleTotalService.findOrderResultByCond(settleQueryDto);
        ExcelUtils excelUtils = new ExcelUtils(orderResultDtos, getHeaderInfo(), getFormatInfo());
        sendHttpResponse(response, "settleTotalReport", excelUtils.getWorkbook());
    }
    @PostMapping("/export")
    @ApiOperation(value = "导出Excel", notes = "导出Excel")
    public void export(@RequestBody SettleCondVo settleCondVo, HttpServletResponse response) {
        SettleQueryDto settleQueryDto=new SettleQueryDto();
        BeanUtils.copyProperties(settleCondVo,settleQueryDto);
        if(StringUtils.isNotEmpty(settleCondVo.getDateBegin())){
            settleQueryDto.setDateBegin(settleCondVo.getDateBegin()+" 00:00:00");
        }
        if(StringUtils.isNotEmpty(settleCondVo.getDateEnd())){
            settleQueryDto.setDateEnd(settleCondVo.getDateEnd()+" 23:59:59");
        }
        // 待导出数据
        List<SettleTotalResultDto> orderResultDtos = settleTotalService.findOrderResultByCond(settleQueryDto);
        ExcelUtils excelUtils = new ExcelUtils(orderResultDtos, getHeaderInfo(), getFormatInfo());
        sendHttpResponse(response, "settleTotalReport", excelUtils.getWorkbook());
    }

    // 获取表头信息
    private List<ExcelHeaderInfo> getHeaderInfo() {
        return Arrays.asList(
                new ExcelHeaderInfo(0, 0, 0, 0, "结算编号"),
                new ExcelHeaderInfo(0, 0, 1, 1, "结算日期"),
                new ExcelHeaderInfo(0, 0, 2, 2, "结算状态"),
                new ExcelHeaderInfo(0, 0, 3, 3, "结算状态名称"),
                new ExcelHeaderInfo(0, 0, 4, 4, "待结算金额"),
                new ExcelHeaderInfo(0, 0, 5, 5, "所属公司"),
                new ExcelHeaderInfo(0, 0, 6, 6, "审核人"),
                new ExcelHeaderInfo(0, 0, 7, 7, "审核日期"),
                new ExcelHeaderInfo(0, 0, 8, 8,  "审核备注")
        );
    }

    // 获取格式化信息
    private Map<String, ExcelFormat> getFormatInfo() {
        Map<String, ExcelFormat> format = new HashMap<>();
       /* format.put("id", ExcelFormat.FORMAT_INTEGER);
        format.put("categoryId", ExcelFormat.FORMAT_INTEGER);
    */
        return null;
    }




}
